const jwt = require('jsonwebtoken');
const omit = require('lodash/omit')
const pick = require('lodash/pick')
const { workorder_collection } = require('../config')
const { staff_collection } = require('../config')
const { db } = require('../config')
const { command } = require('../config')
const _ = command
exports.main = async (event, context) => {
    const data = event;
    const token = data.token
    const secret = 'shhhhh';
    loginInfo = jwt.verify(token, secret)
    if (loginInfo.position != "综调" && loginInfo.position != "班长") {
        return { "code": 0, message: "创建失败，只能综调或班长创建工单", data: null }
    }
    // 1.保存工单数据
    addRes = null;
    const createData = createWordOrderData(loginInfo, data);
    const createRes = await db.collection(workorder_collection).add(createData)
    wordOrder = await db.collection(workorder_collection).doc(createRes.id).get();
    wordOrder = wordOrder.data[0];
    // 2.返回数据
    if (createRes) {
        return { "code": 1, message: "创建成功", data: wordOrder }
    } else {
        return { "code": 0, message: "创建失败", data: null }
    }
}
function createWordOrderData(loginInfo, data) {
    creatorAccount = loginInfo.account
    position = loginInfo.position;
    currentDateTime = getDateTime()
    agreeOrNot = data.agreeOrNot
    approveReason = data.approveReason
    log = [{ "dateTime": currentDateTime, "currentProcess": '工单发起', "position": position, "approver": creatorAccount, "agreeOrNot": agreeOrNot, "approveReason": approveReason }];
    modifyStaff = data.modifyStaff;
    modifyStaff = JSON.parse(modifyStaff);
    addRes = null;
    region = data.region;
    grid = data.grid;
    creatorPosition = loginInfo.position;
    if (creatorPosition == "综调") {// 如果是综调
        type = "综调发起"
        updateStaff(modifyStaff.account);
    } else {// 如果是班长
        type = "班长发起"
    }
    addData = {
        region: region,
        grid: grid,
        creatorAccount: loginInfo.account,
        creator: loginInfo.name,
        creatorName: loginInfo.name,
        creatorId: loginInfo.id,
        createDateTime: currentDateTime,
        currentPosition: position,
        currentProcess: type,
        modifyStaff: data.modifyStaff,
        log: JSON.stringify(log),
        obdAndStaff: data.obdAndStaff || '[]',
        preProcess: '',
        status: "未审批",
        type: type
    }
    return addData;
}
function updateStaff(staffAccount) {
    db.collection(staff_collection).where({ account: staffAccount }).update({ status: "离职" }).then((res) => {
        console.log("员工账号：" + staffAccount + "更新为离职");
        console.log(res, res.data);
    });
}
function getDateTime() {
    var now = new Date();
    // 使用Date对象的方法来获取年、月、日、时、分、秒
    var year = now.getFullYear();
    var month = String(now.getMonth() + 1).padStart(2, '0'); // 月份是从0开始的，所以+1，并确保是两位数
    var day = String(now.getDate()).padStart(2, '0'); // 确保日期是两位数
    var hours = String(now.getHours()).padStart(2, '0'); // 确保小时是两位数，24小时制
    var minutes = String(now.getMinutes()).padStart(2, '0'); // 确保分钟是两位数
    var seconds = String(now.getSeconds()).padStart(2, '0'); // 确保秒是两位数
    // 按照“年月日 时分秒”的顺序拼接成字符串
    var currentDateTime = year + "/" + month + "/" + day + " " + hours + ":" + minutes + ":" + seconds;
    return currentDateTime;
}